package com.hbx.platform.auth.controller;

import com.hbx.platform.auth.domain.EmployeeRole;
import com.hbx.platform.auth.query.EmployeeRoleQuery;
import com.hbx.platform.auth.service.IEmployeeRoleService;
import com.hbx.platform.base.utils.AjaxResult;
import com.hbx.platform.base.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employeeRole")
public class EmployeeRoleController {
    @Autowired
    public IEmployeeRoleService employeeRoleService;


    /**
     * 保存和修改公用的
     * @param employeeRole  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody EmployeeRole employeeRole){
        try {
            if( employeeRole.getId()!=null){
                    employeeRoleService.update(employeeRole);
                    return AjaxResult.me().setMessage("修改成功");
            }else {
                 employeeRoleService.save(employeeRole);
                 return AjaxResult.me().setMessage("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("报错："+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeRoleService.delete(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id")Long id)
    {
        try {
            EmployeeRole employeeRole = employeeRoleService.queryById(id);
            return AjaxResult.me().setData(employeeRole);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }

    @GetMapping("/empId/{id}")
    public AjaxResult queryByEmpId(@PathVariable("id") Long id){
        try {
            EmployeeRole employeeRole = employeeRoleService.queryByEmpId(id);
            return AjaxResult.me().setData(employeeRole);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("报错："+e.getMessage());
        }

    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult getAll(){

        try {
            List< EmployeeRole> list = employeeRoleService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult page(@RequestBody EmployeeRoleQuery query)
    {
        try {
            PageList<EmployeeRole> pageList = employeeRoleService.page(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
}
